﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class MyInputField_MobileInput : MyInputField_TextSelect, IPointerClickHandler, ISelectHandler, IDeselectHandler
{
    protected string m_InputText = "";

    protected bool m_InputFocus = false;

    //软键盘对象
    protected TouchScreenKeyboard m_Keyboard;

    public TouchScreenKeyboardType m_KeyboardType = TouchScreenKeyboardType.Default;

    //是否显示软键盘专属输入框
    public bool m_HideKeyboardInput = false;

    protected override bool IsCaretShow()
    {
        return m_InputFocus; //有输入焦点的时候才显示光标
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button != PointerEventData.InputButton.Left)
            return;

        var go = this.gameObject;
        if (EventSystem.current.currentSelectedGameObject == go)
            OnSelect(null);
        else
            EventSystem.current.SetSelectedGameObject(gameObject, eventData);
    }

    public void OnSelect(BaseEventData eventData)
    {
        m_InputFocus = true;
        m_CaretPos = m_InputText.Length;
        m_SelectPos = m_CaretPos;
        if (TouchScreenKeyboard.isSupported)
        {
            if (Input.touchSupported)
                TouchScreenKeyboard.hideInput = m_HideKeyboardInput;

            m_Keyboard = TouchScreenKeyboard.Open(m_InputText, m_KeyboardType, false, true, false, true, "Input Text");
        }

        CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this); //下一帧显示光标
    }

    public void OnDeselect(BaseEventData eventData)
    {
        m_InputFocus = false;
        if (null != m_Keyboard)
        {
            KeyboardClosed();
        }

        CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this); //下一帧隐藏光标
    }

    private void KeyboardClosed()
    {
        m_InputText = m_Keyboard.text; //软键盘上的输入内容同步过来
        m_Text.text = m_InputText;

        m_Keyboard.active = false;
        m_Keyboard = null;
    }

    protected override void OnProcessInput()
    {
        //do nothing
    }

    void LateUpdate()
    {
        CheckKeyboardInput();
    }

    protected virtual void CheckKeyboardInput()
    {
        if (!m_InputFocus) return;
        if (null == m_Keyboard) return; //软键盘没打开

        if (m_Keyboard.status != TouchScreenKeyboard.Status.Visible) //键盘关掉了
        {
            KeyboardClosed();
            return;
        }

        var str = m_Keyboard.text;
        if (m_InputText != str) //键盘输入内容变了
        {
            m_InputText = "";
            for (int i = 0; i < str.Length; ++i)
            {
                char c = str[i];

                if (0 != c)
                {
                    switch (c)
                    {
                    case '\t': /* tab键 */
                        break;
                    case '\r': /* mac回车键 */
                               //case '\n': /* window回车键 */
                    case (char)3:
                        c = '\n';
                        break;
                    }

                    m_InputText += c;
                }
            }

            if (m_InputText != str)
                m_Keyboard.text = m_InputText;

            if (m_Keyboard.canGetSelection)
            {
                SyncKeyboardInputCaretAndSelectPos();
            }
            else
            {
                m_CaretPos = m_SelectPos = m_InputText.Length; //光标放到最后字符后面
            }

            m_Text.text = m_InputText;
            CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this); //下一帧刷新光标
        }
        else //键盘开着, 输入内容没变时
        {
            if (m_HideKeyboardInput)
            {
                if (m_Keyboard.canSetSelection)
                {
                    m_Keyboard.selection = new RangeInt(m_CaretPos, m_SelectPos - m_CaretPos);
                }
            }
            else
            {
                if (m_Keyboard.canGetSelection)
                {
                    SyncKeyboardInputCaretAndSelectPos();
                }
            }
        }
    }

    //软键盘上的专属输入框的文本选中区域同步到这边
    private void SyncKeyboardInputCaretAndSelectPos()
    {
        var selectionRange = m_Keyboard.selection;
        m_CaretPos = selectionRange.start;
        m_SelectPos = selectionRange.end;
    }

}
